import simpleData from './assets/lottery-data-simple.json'
export interface PageData {
    pageNo: number
    pageSize: number
    pages: number
    total: number
    list: Array<LotteryData>
}

export interface LotteryData {
    drawFlowFund: string
    drawFlowFundRj: string
    drawPdfUrl: string
    estimateDrawTime: string
    isGetKjpdf: number
    isGetXlpdf: number
    lotteryDrawNum: string
    lotteryDrawResult: string // "02 06 07 16 20 02 11"
    lotteryDrawStatus: number // 20
    lotteryDrawStatusNo: string
    lotteryDrawTime: string // "2024-12-04"
    lotteryEquipmentCount: number // 1
    lotteryGameName: string // "超级大乐透"
    lotteryGameNum: string // "85"
    lotteryGamePronum: number
    lotteryNotice: number
    lotteryNoticeShowFlag: number
    lotteryPaidBeginTime: string // "2024-12-04 23:30:01"
    lotteryPaidEndTime: string // "2025-02-03 23:59:59"
    lotteryPromotionFlag: number //  
    lotteryPromotionFlagRj: number
    lotterySaleBeginTime: string //"2024-12-02 21:10:00"
    lotterySaleEndTimeUnix: number // 0
    lotterySaleEndtime: string // "2024-12-04 21:00:00"
    lotterySuspendedFlag: number // 0
    lotteryUnsortDrawresult: string // "07 16 02 20 06 02
}

export function getPageData(pageNo: number, pageSize: number): Promise<PageData> {
    return fetch(
        `https://webapi.sporttery.cn/gateway/lottery/getHistoryPageListV1.qry?gameNo=85&provinceId=0&pageSize=${pageSize}&isVerify=1&pageNo=${pageNo}`
    ).then((res) =>
        res.json()
    ).then((data) => {
        if (data.errorCode != '0') {
            throw new Error(data.errorMsg)
        }
        
        console.log(data.value)
        return data.value as PageData
    })
}

export async function getAllData() {
    let pageNo = 1
    const pageSize = 100
    let data: LotteryData[] = []
    
    while (true) {
        console.log(`pageNo: ${pageNo}`)
        const page = await getPageData(pageNo, pageSize) 
        if (page.list.length < pageSize || pageNo >= page.pages) {
            data = data.concat(page.list)
            break    
        }

        data = data.concat(page.list)
        pageNo++
    } 

    return data
}

export async function getPie() {
    const pie = []
   for (const item of  simpleData) {
       pie.push([item.lotterDrawNum, item.lotteryDrawResult])
   }

   return pie
}